﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

public partial class BUserAndGroupM : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();
    private string errorTimeOutUrl = "../../error.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, errorTimeOutUrl);
        if (!IsPostBack)
        { 
            UserAndGroupe(Session["UserCode"].ToString()); 
        }
    }

    #region 初始化绑定 UserAndGroupe(string UserCode)
    protected void UserAndGroupe(string UserCode)
    {   
        //权限
        comHelp.GetRightUnite(this, new string[] { UserCode, Session["CoCode"].ToString(), "BUserAndGroup" }, new string[] { "Button1", "Button2", "Button3", "Button4" }, new int[] { 1, 3, 3, 1 }, "../../SLogin.aspx");
        //绑定用户组
        UserGroup();
    }
    
    // 绑定用户组
    protected void UserGroup()
    {
        string StrUserGroupSql = " select UGCode,UGName from B_UserGroup where DeleteFlag='0' ";
        ddlUserGroupName.DataTextField = "UGName";
        ddlUserGroupName.DataValueField = "UGCode";
        ddlUserGroupName.DataSource = help.GetDataTable(StrUserGroupSql);
        ddlUserGroupName.DataBind();

        UserNotInGroupBind(ddlUserGroupName.SelectedItem.Value.ToString());
    }
    #endregion

    //分别绑定未在此组的用户和在组的用户的列表
    protected void UserNotInGroupBind(string UGCode)
    {
        //未在组里的
        string StrWNGSql = " select U.UserCode,U.UserEngName+'-'+U.UserFullName UserName "
            + " from B_User U where U.DeleteFlag=0 and U.UserCode not in (select UG.UserCode from B_UserAndGroup UG  "
            + " where UG.UGCode='" + UGCode + "') order by U.UserEngName ";
        lbxUserNotInGroup.DataSource = help.GetDataTable(StrWNGSql);
        lbxUserNotInGroup.DataValueField = "UserCode";
        lbxUserNotInGroup.DataTextField = "UserName";
        lbxUserNotInGroup.DataBind();

        //在组里的
        string StrWGSql = " select U.UserCode,U.UserEngName+'-'+U.UserFullName UserName "
            + " from B_User U join B_UserAndGroup UG on U.UserCode=UG.UserCode "
            + " where U.DeleteFlag=0 and UG.UGCode='" + UGCode + "'  order by U.UserEngName ";
        DataTable Dt = help.GetDataTable(StrWGSql);
        lbxUserInGroup.DataSource = Dt;
        lbxUserInGroup.DataValueField = "UserCode";
        lbxUserInGroup.DataTextField = "UserName";
        lbxUserInGroup.DataBind();
        //作为曾、删的对比
        lbxInsert2.DataSource = Dt;
        lbxInsert2.DataValueField = "UserCode";
        lbxInsert2.DataTextField = "UserName";
        lbxInsert2.DataBind();
    }

    //根据用户组动态绑定
    protected void ddlUserGroupeName_TextChanged(object sender, EventArgs e)
    {
        lbxDelete.Items.Clear();
        lbxInsert.Items.Clear();
        lbxInsert2.Items.Clear();
        UserNotInGroupBind(ddlUserGroupName.SelectedItem.Value.ToString());
    }

    #region 用户分组
    //单项添加
    protected void Button1_Click(object sender, EventArgs e)
    {
        //不在组里的用户数
        //int InNum = lbxWebNotInGroup.Items.Count;
        for (int i = 0; i < lbxUserNotInGroup.Items.Count; i++)
        {
            if (lbxUserNotInGroup.SelectedIndex >= 0)
            {
                lbxUserInGroup.Items.Add(lbxUserNotInGroup.SelectedItem);

                //是否插入
                int INum = 0;
                string StrUserName = lbxUserNotInGroup.SelectedItem.Text.ToString();

                for (int d = 0; d < lbxInsert2.Items.Count; d++)
                {
                    if (StrUserName.Equals(lbxInsert2.Items[d].Text.ToString()))
                    { ++INum; }
                }

                if (INum < 1)
                { lbxInsert.Items.Add(lbxUserNotInGroup.SelectedItem); }

                lbxUserNotInGroup.Items.Remove(lbxUserNotInGroup.SelectedItem);
                lbxDelete.Items.Remove(lbxUserNotInGroup.SelectedItem);
            }
        }
    }
    //单项移除
    protected void Button2_Click(object sender, EventArgs e)
    {
        //不在组里的用户数
        //int InNum = lbxWebInGroup.Items.Count;
        for (int i = 0; i < lbxUserInGroup.Items.Count; i++)
        {
            if (lbxUserInGroup.SelectedIndex >= 0)
            {
                lbxUserNotInGroup.Items.Add(lbxUserInGroup.SelectedItem);
                //插入行
                int INum = 0;
                //用户类型
                string WebTypeStr = lbxUserInGroup.SelectedItem.Text.ToString();

                for (int k = 0; k < lbxInsert2.Items.Count; k++)
                {
                    if (WebTypeStr.Equals(lbxInsert2.Items[k].Text.ToString()))
                    {
                        ++INum;
                    }
                }
                if (INum > 0)
                {
                    lbxDelete.Items.Add(lbxUserInGroup.SelectedItem);
                }
                lbxInsert.Items.Remove(lbxUserInGroup.SelectedItem);
                lbxUserInGroup.Items.Remove(lbxUserInGroup.SelectedItem);
            }
        }
    }
    //全部添加
    protected void Button4_Click(object sender, EventArgs e)
    {
        //在删除组的
        for (int j = 0; j < lbxDelete.Items.Count; j++)
        {
            lbxUserNotInGroup.Items.Remove(lbxDelete.Items[j]);
            lbxUserInGroup.Items.Add(lbxDelete.Items[j]);
        }
        //不在组里的用户数
        for (int i = 0; i < lbxUserNotInGroup.Items.Count; i++)
        {
            lbxUserInGroup.Items.Add(lbxUserNotInGroup.Items[i]);
            lbxInsert.Items.Add(lbxUserNotInGroup.Items[i]);
        }
        lbxUserNotInGroup.Items.Clear();
        lbxDelete.Items.Clear();
    }
    //全部删除
    protected void Button3_Click(object sender, EventArgs e)
    {
        //在添加组的
        for (int k = 0; k < lbxInsert.Items.Count; k++)
        {
            lbxUserNotInGroup.Items.Add(lbxInsert.Items[k]);
            lbxUserInGroup.Items.Remove(lbxInsert.Items[k]);
        }
        //不在组里的用户数
        for (int i = 0; i < lbxUserInGroup.Items.Count; i++)
        {
            lbxUserNotInGroup.Items.Add(lbxUserInGroup.Items[i]);
            lbxDelete.Items.Add(lbxUserInGroup.Items[i]);
        }
        lbxUserInGroup.Items.Clear();
        lbxInsert.Items.Clear();
    }
    #endregion

    //保存
    protected void Button5_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, errorTimeOutUrl);
        string Str = string.Empty;//操作字符串
        //用户编号
        string UGCode = ddlUserGroupName.SelectedItem.Value.ToString();
        
        //当前用户
        string UserName = Session["UserCode"].ToString();
        //当前日期
        string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
        //统计插入与删除的行数
        int InsertN = lbxInsert.Items.Count;
        int DeleteN = lbxDelete.Items.Count;

        //插入
        if (InsertN > 0)
        {   //插入SQL语句前部分
            string InsertStr = " Insert Into B_UserAndGroup(DeleteFlag,AddAuthor,"
            + "AddDate,UpdateAuthor,UpdateDate,UGCode,UserCode) values('0','" + UserName
            + "','" + StrDate + "','" + UserName + "','" + StrDate + "','" + UGCode + "','";

            for (int i = 0; i < InsertN; i++)
            {
                Str += InsertStr + lbxInsert.Items[i].Value.ToString() + "');  ";
            }
        }
        //删除
        if (DeleteN > 0)
        {   //删除SQL语句前部分
            string DeleteStr = " Delete from B_UserAndGroup where UGCode='"
                + UGCode + "' and UserCode='";
            for (int j = 0; j < DeleteN; j++)
            {
                Str += DeleteStr + lbxDelete.Items[j].Value.ToString() + "';   ";
            }
        }

        if (!string.IsNullOrEmpty(Str))
        {
            if (help.RunSqlInt(Str) > 0)
            {
                CommonMethod.regAlertMessage("操作成功！", this);
                UserNotInGroupBind(ddlUserGroupName.SelectedItem.Value.ToString());
            }
        }
    }
    
    //查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        //未在组里的
        string StrWNGSql = " select U.UserCode,U.UserEngName+'-'+U.UserFullName UserName "
            + " from B_User U where U.UserCode not in (select UG.UserCode from B_UserAndGroup UG  "
            + " where UG.UGCode='" + ddlUserGroupName.SelectedValue.ToString() 
            + "') and (U.UserCode like '%" + txtCondition.Text.ToString() + "%' "
            + " OR U.UserName like '%" + txtCondition.Text.ToString() + "%' OR U.UserFullName like '%"
            + txtCondition.Text.ToString() + "%' OR U.UserEngName like '%"
            + txtCondition.Text.ToString() + "%')  order by U.UserEngName ";
        lbxUserNotInGroup.DataSource = help.GetDataTable(StrWNGSql);
        lbxUserNotInGroup.DataValueField = "UserCode";
        lbxUserNotInGroup.DataTextField = "UserName";
        lbxUserNotInGroup.DataBind();
    }

    //导出
    protected void cmdExportToExcel_Click(object sender, EventArgs e)
    {
        //在组里的
        string StrWGSql = " select U.UserCode 用户编号,U.UserFullName+'-'+U.UserEngName 用户名 "
            + " from B_User U join B_UserAndGroup UG on U.UserCode=UG.UserCode "
            + " where U.DeleteFlag=0 and UG.UGCode='" + ddlUserGroupName.SelectedValue.ToString() + "' order by U.UserEngName ";
        DataTable Dt = help.GetDataTable(StrWGSql);
        string path = Server.MapPath("ExproteTempFile") + @"\信息表.xls";
        //HWobject.ExportExcel(Dt, path);
        //Response.Redirect("FrmDownFile.aspx?url=" + path);
    }
    private void Export(string FileType, string FileName, DataTable Dt)
    {
        GridView ExcelGrv = new GridView();
        ExcelGrv.DataSource = Dt;
        ExcelGrv.DataBind();
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        ExcelGrv.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
}
